package com.demo2do.fms.admin.web.controller.supplier.sub.flow;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.resolver.Preloading;
import com.demo2do.core.web.utils.WebUtils;
import com.demo2do.fms.admin.entity.search.supplier.DriverAuditSearch;
import com.demo2do.fms.admin.entity.security.Login;
import com.demo2do.fms.admin.service.supplier.DriverService;
import com.demo2do.fms.admin.service.supplier.SupplierService;
import com.demo2do.fms.admin.web.resolver.Secure;
import com.demo2do.fms.entity.driver.Driver;
import com.demo2do.fms.entity.driver.DriverAuditStatus;
import com.demo2do.fms.entity.supplier.common.Supplier;
import com.demo2do.fms.entity.supplier.common.Type;

/**
 * 分属供方司机审核
 * 
 * 一级供方可以看到所属他的所有二级供方的司机，可以对其申报进行通过或驳回
 * 分区可以看到所属他的一级供方的司机，可以对其申报进行通过或驳回
 * 总部可以看到所有的供方列表，不过总部一般不对其申报进行通过或驳回
 * 
 * @author dan
 *
 */
@Controller
@RequestMapping("/driver-audits")
@MenuSetting("menu-supplier")
public class DriverAuditController {
    
    private static final Logger logger = LoggerFactory.getLogger(DriverAuditController.class);
    
    @Autowired
    private SupplierService supplierService;
    
    @Autowired
    private DriverService driverService;
    
    /**
     * 
     * @param login
     * @param page
     * @param request
     * @return
     */
    @RequestMapping(value = "" , method = RequestMethod.GET)
    @MenuSetting("submenu-auditing-driver-management")
    public ModelAndView list(@Secure Login login, Page page, HttpServletRequest request){
        
        ModelAndView modelAndView = new ModelAndView("supplier/sub/flow/driver/driver-audit-list");
        
        List<Long> supplierIds = supplierService.listSubSupplierId(login);
        
        int totals = driverService.count(supplierIds, DriverAuditStatus.AUDIT);
        if(totals > 0) {
            
            List<Driver> drivers = driverService.list(supplierIds, DriverAuditStatus.AUDIT, page.with(totals));
            
            modelAndView.addObject("drivers", drivers);
            modelAndView.addObject("page", page);
            modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page", "filter"));
            
        }
        
        return modelAndView;
        
    }
    
    /**
     * @param login
     * @return
     */
    @RequestMapping(value = "/search" , method = RequestMethod.GET )
    @MenuSetting("submenu-auditing-driver-management")
    public ModelAndView search(@Secure Login login) {
        
        ModelAndView modelAndView = new ModelAndView("supplier/sub/flow/driver/driver-audit-search");
        
        List<Supplier> suppliers = supplierService.listSubSupplier(login, Type.MOTOR);
        modelAndView.addObject("suppliers", suppliers);
        
        return modelAndView;
        
    }
    
    /**
     * 
     * @param login
     * @param driverSearch
     * @param request
     * @param page
     * @return
     */
    @RequestMapping(value = "" , method = RequestMethod.GET  , params = "filter")
    @MenuSetting("submenu-auditing-driver-management")
    public ModelAndView onSearch(@Secure Login login, DriverAuditSearch driverSearch,
                                 HttpServletRequest request, Page page){
        
        ModelAndView modelAndView = new ModelAndView("supplier/sub/flow/driver/driver-audit-list");
        
        if (login.isSupplier()) {
            List<Long> supplierIds = supplierService.listSubSupplierId(login);
            driverSearch.addSupplierIds(supplierIds);
        }
        
        int totals = driverService.count(driverSearch);
        if (totals > 0) {
            
            List<Driver> drivers = driverService.search(driverSearch, page.with(totals));
            
            modelAndView.addObject("drivers", drivers);
            modelAndView.addObject("page", page);
            modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page", "filter"));
            
        }
        
        return modelAndView;
        
    }
    
    /**
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @MenuSetting("submenu-auditing-driver-management")
    public ModelAndView view(@PathVariable("id") Long id) {

        ModelAndView modelAndView = new ModelAndView("supplier/sub/flow/driver/driver-audit-view");
        
        Driver driver = driverService.load(id);
        modelAndView.addObject("driver", driver);

        return modelAndView;
        
    }
    
    /**
     * 
     * @param driverId
     * @return
     */
    @RequestMapping(value = "/{id}/adopt" , method = RequestMethod.GET)
    public ModelAndView adopt(@PathVariable("id") Long driverId){
        
        ModelAndView modelAndView = new ModelAndView("supplier/sub/flow/driver/driver-audit-adopt");
        
        modelAndView.addObject("driverId", driverId);
        
        return modelAndView;
    }

    /**
     * @param driver
     * @param operator
     * @return
     */
    @RequestMapping(value = "/{id}/adopt" , method = RequestMethod.PATCH , produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onAdopt(@Preloading("driverId") Driver driver, @Secure(property = "id") Long operator){
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            driverService.adopt(driver, operator);
            
            httpResult.success().message("审核通过成功");
            
        } catch (Exception e) {
            logger.error("onAdopt - adopt fail, exception = [{}]", e);
            httpResult.fail("500").message("审核通过失败");
        }
        
        return httpResult;
    }
    
    /**
     * 
     * @param driverId
     * @return
     */
    @RequestMapping(value = "/{id}/reject" , method = RequestMethod.GET)
    public ModelAndView reject(@PathVariable("id") Long driverId){
        
        ModelAndView modelAndView = new ModelAndView("supplier/sub/flow/driver/driver-audit-reject");
        
        modelAndView.addObject("driverId", driverId);
        
        return modelAndView;
    }

    /**
     * @param driver
     * @param operator
     * @return
     */
    @RequestMapping(value = "/{id}/reject" , method = RequestMethod.PATCH , produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onReject(@Preloading("driverId") Driver driver, @Secure(property = "id") Long operator){
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            driverService.reject(driver, operator);
            
            httpResult.success().message("驳回成功");
            
        } catch (Exception e) {
            logger.error("onReject - reject fail, exception is [{}]", e);
            httpResult.fail("500").message("驳回失败");
        }
        
        return httpResult;
    }
}
